﻿Imports utileria
Imports DataSet_apa
Imports DataSet_apaTableAdapters





Partial Class Registro_buscar
    Inherits System.Web.UI.Page
    Const sEditFiltroPage As String = "~/Reportes/EdicionFiltro.aspx?opt=1&modulo=10&filtro="

    Protected Sub Registro_buscar_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        ' Inicializar la caja de texto
        lblSQL.Text = ""
        lblFiltroNombre.Text = ""
        If Not IsPostBack Then
            If IsNothing(Session("filtro")) Then ' Si existe un filtro aplicado activamos la opcion
                chkFiltro.Enabled = False
                lkEdicionFiltro.Visible = False
            Else
                chkFiltro.Checked = True
                lkEdicionFiltro.Visible = True
            End If
        End If


    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim utiles As New utileria
        Dim sWhere As String = ""
        Dim sSQLBasic = "SELECT AnimalId, sexo, numero, letra, ano, nombre, pelaje1," & _
                            " hierro, photo,estatus, ubicacion, fechanac," & _
                            " porcien_cs, no_padre, ltra_padre, ano_padre, id_padre, hie_padre," & _
                            " ano_madre, ltra_madre, no_madre, id_madre, hie_madre, abuelo_mat," & _
                            " tienta_ac, tienta_ap, lidia_ap, lidia_ac, estatus, fechabaja, " & _
                            "tienta_fecha, tienta_torero, hijos, hijas, propiedad FROM animal AS m "
        Dim sSQL As String = ""

        sSQL = sSQLBasic
        lblSQL.Text = ""
        If Not IsNothing(Session("filtro")) Then ' Si existe filtro aplicado
            If chkFiltro.Checked Then ' S el usuario solicitó la aplicacion del filtro
                sWhere = utiles.sWhereFiltro(Session("filtro")) ' Generó la cadena WHERE
                Dim NombreFiltro As New filtroDataTable
                Dim _NombreFiltro As New filtroTableAdapter
                NombreFiltro = _NombreFiltro.GetFiltroById(Session("filtro"))
                lblFiltroNombre.Text = NombreFiltro(0).nombreFiltro
                pnFiltro.CssClass = "warning"
                lkEdicionFiltro.NavigateUrl = sEditFiltroPage & Session("filtro").ToString


                If Not sWhere = "" Then ' Si hay datos concatena con el SQL
                    sSQL &= sWhere
                    lblSQL.Text = sWhere
                End If
            End If
        End If
        sSQL &= " Order By numero, letra,ano,sexo;"  ' Genera la ordernacion incial de los registros
        sqlGeneral.SelectCommand = sSQL


        Try
            grvCrias.DataBind()
        Catch ex As Exception
            lblSQL.CssClass = "error"
            lblSQL.Text = "<b>OCURRIO UN ERROR AL APLICAR EL FILTRO</b>, verifique la informacion: " + lblSQL.Text
            sqlGeneral.SelectCommand = sSQLBasic
            grvCrias.DataBind()
        End Try

    End Sub

    Protected Sub sqlGeneral_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles sqlGeneral.Selected
        ' Muestra el numero de registros obtenidos
        lblRegistros.Text = e.AffectedRows.ToString
    End Sub

#Region "Opciones de filtrado"
    Protected Sub txtAnimal_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAnimal.TextChanged
        sqlGeneral.FilterExpression = "numero+letra+ano LIKE ('" & txtAnimal.Text & "%')"
        txtMadre.Text = ""
        txtPadre.Text = ""
    End Sub

    Protected Sub txtPadre_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPadre.TextChanged
        sqlGeneral.FilterExpression = "no_padre + ltra_padre + ano_padre  LIKE ('" & txtPadre.Text & "%')"
        txtAnimal.Text = ""
        txtMadre.Text = ""
    End Sub

    Protected Sub txtMadre_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMadre.TextChanged
        sqlGeneral.FilterExpression = "no_madre + ltra_madre + ano_madre  LIKE ('" & txtMadre.Text & "%')"
        txtPadre.Text = ""
        txtAnimal.Text = ""
    End Sub
#End Region

    Protected Sub chkFiltro_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkFiltro.CheckedChanged
        txtAnimal.Text = ""
        txtMadre.Text = ""
        txtPadre.Text = ""
    End Sub

    Protected Sub btnLiberar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnLiberar.Click
        Session("filtro") = Nothing
        chkFiltro.Checked = False
        chkFiltro.Enabled = False
        lblFiltroNombre.Text = ""
        lblSQL.Text = ""
        pnFiltro.CssClass = ""
        lkEdicionFiltro.Visible = False

    End Sub
End Class
